package com.tydic.springboot.excel.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.tydic.springboot.excel.domain.SysDictItem;
import com.tydic.springboot.excel.domain.SysDictPage;
import com.tydic.springboot.excel.domain.report.*;
import com.tydic.springboot.service.StudentSertvice;
import com.tydic.springboot.util.HttpServletUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("easyPoi")
public class ExcelPoiController {
    @Autowired
    private StudentSertvice studentSertvice;

    @GetMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        List<SysDictItem> dictItems = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            SysDictItem dictItem = new SysDictItem("ID_" + i, "子级_" + i, "VALUE" + i);
            dictItems.add(dictItem);
        }

        // 创建导出参数
        ExportParams exportParams = new ExportParams("用户订单列表", "用户订单");

        // 创建导出对象
        SysDictPage dictPage = new SysDictPage("ID_MAIN", "父级", "parent");
        dictPage.setSysDictItemList(dictItems);
        List<SysDictPage> list = new ArrayList<>();
        list.add(dictPage);
        // 导出Excel文件
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SysDictPage.class, list);

        // 导出Excel文件
        try (OutputStream os = response.getOutputStream()) {
            HttpServletUtil.setResponse(request, response, "用户订单列表.xls");
            workbook.write(os);
            os.flush();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/readExcel")
    public Object readExcel(@RequestParam("file") MultipartFile file) throws Exception {
        //导入配置对象
        ImportParams importParams = new ImportParams();
        //标题占了几行
        importParams.setTitleRows(1);
        //字段标题占了几行
        importParams.setHeadRows(2);
        // excel文件，导入的类型，导入的配置对象
        List<SysDictPage> list = ExcelImportUtil.importExcel(file.getInputStream(), SysDictPage.class, importParams);
        return list;
    }

    /**
     * 导出 批次指标各阶段数据统计
     */
    @RequestMapping(value = "/exportIndexBatchDetailResultConfirm", method = RequestMethod.GET)
    public void exportIndexBatchDetailResultConfirm(HttpServletRequest request, HttpServletResponse response) {
        List<ReportIndexStateDetailExportVO> exportList = getReportIndexStateDetailExports();

        // 设置导出参数
        Workbook workBook = ExcelExportUtil.exportExcel(new ExportParams(), ReportIndexStateDetailExportVO.class, exportList);
        workBook.setSheetName(0, "批次指标各阶段数据明细");

        try (OutputStream os = response.getOutputStream()) {
            HttpServletUtil.setResponse(request, response, "批次指标各阶段数据明细.xls");
            workBook.write(os);
            os.flush();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private List<ReportIndexStateDetailExportVO> getReportIndexStateDetailExports() {
        List<ReportIndexStateDetailExportVO> exportList = new ArrayList<>();
        ReportIndexStateDetailExportVO exportVO = new ReportIndexStateDetailExportVO();
        exportVO.setRegion("北京");
        exportVO.setBatchName("第一批次");

        //处理数据
        List<ReportDemandExportVO> demandList = getReportDemandExports();
        exportVO.setDemandList(demandList);//需求阶段
        List<ReportSiteExportVO> siteList = getReportSiteExports();
        exportVO.setSiteList(siteList);//站址筛查
        List<ReportOrderExportVO> orderList = new ArrayList<>();
        exportVO.setOrderList(orderList);//订单阶段
        List<ReportHandOverExportVO> handOverList = new ArrayList<>();
        exportVO.setHandOverList(handOverList);//交付阶段
        exportList.add(exportVO);

        exportVO = new ReportIndexStateDetailExportVO();
        exportVO.setRegion("武汉");
        exportVO.setBatchName("第一批次");
        exportList.add(exportVO);
        return exportList;
    }

    private List<ReportSiteExportVO> getReportSiteExports() {
        List<ReportSiteExportVO> siteList = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            ReportSiteExportVO vo = new ReportSiteExportVO();
            vo.setSiteCoAddressNum((i + 1) * 10);
            vo.setSiteNewAddressExclusiveNum((i + 1) * 20);
            vo.setSiteNewAddressSharedNum((i + 1) * 30);
            vo.setSiteShareRateCurr(String.valueOf((i + 1) * 10));
            siteList.add(vo);
        }
        return siteList;
    }

    private List<ReportDemandExportVO> getReportDemandExports() {
        List<ReportDemandExportVO> demandList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ReportDemandExportVO vo = new ReportDemandExportVO();
            vo.setDemandCoAddressNum((i + 1) * 10);
            vo.setDemandNewAddressNum((i + 1) * 20);
            demandList.add(vo);
        }
        return demandList;
    }
}
